const Mock=require('mockjs');

let db=Mock.mock({
    'data|9-10':[{
        id:'@id',
        username:'@name',
        'age|18-32':1,
        email:'@EMAIL',
        phone:/\d{11}/,
        website:Mock.Random.url('http')
    }],
    errorCode:0
});
let User=null;
module.exports= {
    'GET /user/check':(req,res)=>{
        res.status(401).json({
            errorCode:49,message:'请登录'
        })
    },
    'GET /user': (req, res) => { 
        res.status(200).json(db)
    },
    'POST /user':(req,res)=>{
        let user=req.body;
        user.id=Mock.mock('@id');
        db.data.push(user);
        res.status(200).json(user);
    },
    ['POST /user/login'](req,res){
        if(req.body.userName!='star'){
            res.status(200).json({errorCode:1,message:'账户或密码错误'});
            return ;
        }
        let user={name:req.body.userName};
        user.id=Mock.mock('@id');
        user.token=Mock.mock('32')
        User=user;
        var t=new Date();
        t.setDate(t.getDate()+1);
        User.exp=t;
        res.status(200).json({errorCode:0,data:User});
    },
    'GET /user/info':(req,res)=>{
        if(User && User.name){
        //if(Math.random()>0.5){
            res.status(200).json({errorCode:0,data:User})
        }else{
            res.status(200).json({errorCode:1,message:'请登录',data:User})
        }
        //}else{
            //res.status(200).json({errorCode:1,message:'请登录',data:User})
        //}
    },
    'GET /user/fetchMessageCount':(req,res)=>{
        res.status(200).json({
            errorCode:0,data:{
            count: 12
        }})
    },
    'POST /users/register':(req,res)=>{
        console.log(req.body);
        res.status(200).json({errorCode:0,message:'注册成功'})
    }
}